package com.luxsoft.siipap.em.replica.ui.tasks;

import java.awt.event.ActionEvent;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;

import com.jgoodies.binding.value.ValueHolder;
import com.jgoodies.forms.builder.ButtonBarBuilder;
import com.jgoodies.uif.AbstractDialog;
import com.jgoodies.uif.component.UIFButton;


import com.luxsoft.siipap.em.replica.importadores.ImportError;
import com.luxsoft.siipap.em.replica.importadores.ImportacionCanceledException;
import com.luxsoft.siipap.em.replica.importadores.cxc.ImportadorDeAplicacionesANotasDeCargo2;


import com.luxsoft.siipap.model.Periodo;
import com.luxsoft.siipap.swing.binding.Binder;


/**
 * Componente grafico para la migrar Notas de Cargo por pagar Oracle
 * 
 * @author Ruben Cancino
 *
 */
public class ImportadorAplicacionesDeNotasDeCargoTask {

	public ImportadorAplicacionesDeNotasDeCargoTask() {
		
	}
	
	public void execute(){		
		FacTaskDialog dialog=new FacTaskDialog();
		final SwingWorker worker=new Importador(dialog);
		dialog.registerWorker(worker);
		dialog.open();
		printResult();
	}
	
	protected void printResult(){
		
	}
	
	private class FacTaskDialog extends TaskDialog{
		
		protected Periodo periodo=Periodo.getPeriodoDelMesActual();
		
		
		
		public FacTaskDialog() {
			super("Importador de  Aplicaciones a Notas de Cargo");
		}
		
		protected JComponent buildButtonBar() {
			getHeader().setTitle("Periodo: "+periodo.toString());
			ButtonBarBuilder builder =new ButtonBarBuilder();
			builder.addGlue();			
			builder.addGridded(createCambioPeriodoBtn());
			builder.addUnrelatedGap();
			builder.addGridded(createStartButton(true));
			builder.addRelatedGap();
			builder.addGridded(createStopButton());
	        builder.addUnrelatedGap();
	        builder.addGridded(createDetailButton());
	        return builder.getPanel();        
		}
		
		
		
		protected JButton createCambioPeriodoBtn(){
			Action cambiarPeriodo=new AbstractAction("Periodo"){
				public void actionPerformed(ActionEvent e) {
					ValueHolder holder=new ValueHolder(periodo);
					AbstractDialog dialog=Binder.createPeriodoSelector(holder);
					dialog.open();
					if(!dialog.hasBeenCanceled()){
						periodo=(Periodo)holder.getValue();
						getHeader().setTitle("Periodo: "+periodo.toString());
					}
					dialog.dispose();
				}
			};
			UIFButton btn=new UIFButton(cambiarPeriodo);
			return btn;
		}
		
	}
	
	private class Importador extends SwingWorker<String,String>{
		
		private final FacTaskDialog dialog;
		
		public Importador(final FacTaskDialog dialog){
			this.dialog=dialog;
		}

		@Override
		protected String doInBackground() throws Exception {			
			dialog.getHeader().setTitle("Importando aplicaciones a notas de cargo para el periodo"+dialog.periodo);
			importador.importar(dialog.periodo);
			return "OK";
		}

		@Override
		protected void process(List<String> chunks) {
			for(String s:chunks){
				dialog.infoArea.append(s+"\n");
			}
		}

		@Override
		protected void done() {
			dialog.progress.setValue(100);
			super.done();
		}

		ImportadorDeAplicacionesANotasDeCargo2 importador=new ImportadorDeAplicacionesANotasDeCargo2(){

			@Override
			public void setProgress(int progress) {
				System.out.println("Progreso: "+progress);
				Importador.this.setProgress(progress);
			}
			
			
			@Override
			protected void updateCurrent() {
				if(!isCancelled())
					super.updateCurrent();
				else{
					dialog.infoArea.append("\nTarea cancelada");
					throw new ImportacionCanceledException("Tarea cancelada");
				}
			}

			@Override
			public void addError(ImportError err) {					
				super.addError(err);
				dialog.infoArea.append("\n"+err.toString());
			}
			
			
		};			
		
		
	}
	
	public static void main(String[] args) throws InterruptedException, InvocationTargetException {	
		SwingUtilities.invokeAndWait(new Runnable(){
			public void run() {
				new ImportadorAplicacionesDeNotasDeCargoTask().execute();
				
			}
			
		});
		
	}

}
